package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.mapper.OccupantMapper;
import com.bayihouse.model.entity.Occupant;
import com.bayihouse.model.request.occupant.OccupantQueryRequest;
import com.bayihouse.model.vo.OccupantVO;
import com.bayihouse.service.OccupantService;
import com.bayihouse.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【occupant】的数据库操作Service实现
 * @createDate 2024-09-17 10:28:03
 */
@Service
public class OccupantServiceImpl extends ServiceImpl<OccupantMapper, Occupant>
        implements OccupantService {

    public static final int DEFAULT_BATCH_SIZE = 10000;

    @Override
    public OccupantVO getOccupantVO(Occupant occupant) {
        OccupantVO occupantVO = new OccupantVO();
        if (occupant == null) {
            return occupantVO;
        }
        try {
            return OccupantVO.objToVO(occupant);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Wrapper<Occupant> getQueryWrapper(OccupantQueryRequest occupantQueryRequest) {
        QueryWrapper<Occupant> queryWrapper = new QueryWrapper<>();
        Long id = occupantQueryRequest.getId();
        String name = occupantQueryRequest.getName();
        String identity = occupantQueryRequest.getIdentity();
        Integer sex = occupantQueryRequest.getSex();
        String ethnicity = occupantQueryRequest.getEthnicity();
        String idType = occupantQueryRequest.getIdType();
        Integer minAge = occupantQueryRequest.getMinAge();
        Integer maxAge = occupantQueryRequest.getMaxAge();
        String telephone = occupantQueryRequest.getTelephone();
        Integer isMarriage = occupantQueryRequest.getIsMarriage();
        String education = occupantQueryRequest.getEducation();
        String polOutlook = occupantQueryRequest.getPolOutlook();
        String nickname = occupantQueryRequest.getNickname();
        Double height = occupantQueryRequest.getHeight();
        String somatotype = occupantQueryRequest.getSomatotype();
        String face = occupantQueryRequest.getFace();
        String bloodType = occupantQueryRequest.getBloodType();
        List<String> label = occupantQueryRequest.getLabel();
        Long roomId = occupantQueryRequest.getRoomId();
        String role = occupantQueryRequest.getRole();
        String address = occupantQueryRequest.getAddress();
        String province = occupantQueryRequest.getProvince();
        String city = occupantQueryRequest.getCity();
        String street = occupantQueryRequest.getStreet();
        String mannageStreet = occupantQueryRequest.getMannageStreet();
        String detail = occupantQueryRequest.getDetail();
        String jieyumeasure = occupantQueryRequest.getJieyumeasure();
        Long administerId = occupantQueryRequest.getAdministerId();
        String nowAddress = occupantQueryRequest.getNowAddress();
        Long addUserId = occupantQueryRequest.getAddUserId();
        String work = occupantQueryRequest.getWork();
        Integer childNum = occupantQueryRequest.getChildNum();
        String supportNumber = occupantQueryRequest.getSupportNumber();
        String certificateNumber = occupantQueryRequest.getCertificateNumber();
        String accountType = occupantQueryRequest.getAccountType();
        Date stayTime = occupantQueryRequest.getStayTime();
        String workplace = occupantQueryRequest.getWorkplace();
        String district = occupantQueryRequest.getDistrict();
        String sortField = occupantQueryRequest.getSortField();
        String sortOrder = occupantQueryRequest.getSortOrder();
        Integer status = occupantQueryRequest.getStatus();

        if (CollectionUtils.isNotEmpty(label)) {
            for (String tag : label) {
                queryWrapper.like("label", "\"" + tag + "\"");
            }
        }
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(StringUtils.isNotBlank(identity), "identity", identity);
        queryWrapper.eq(sex != null, "sex", sex);
        if (maxAge != null) {
            queryWrapper.ge(maxAge != null, "birthday", transfer(maxAge));

        }
        if (minAge != null) {
            queryWrapper.le(minAge != null, "birthday", transfer(minAge));

        }
        queryWrapper.eq(StringUtils.isNotBlank(telephone), "telephone", telephone);
        queryWrapper.eq(isMarriage != null, "isMarriage", isMarriage);
        queryWrapper.eq(StringUtils.isNotBlank(education), "education", education);
        queryWrapper.eq(StringUtils.isNotBlank(polOutlook), "polOutlook", polOutlook);
        queryWrapper.eq(StringUtils.isNotBlank(nickname), "nickname", nickname);
        queryWrapper.eq(height != null, "height", height);
        queryWrapper.eq(StringUtils.isNotBlank(somatotype), "somatotype", somatotype);
        queryWrapper.eq(StringUtils.isNotBlank(face), "face", face);
        queryWrapper.eq(StringUtils.isNotBlank(bloodType), "bloodType", bloodType);
        queryWrapper.eq(roomId != null, "roomId", roomId);
        queryWrapper.eq(role != null, "role", role);
        queryWrapper.eq(StringUtils.isNotBlank(address), "address", address);
        queryWrapper.eq(StringUtils.isNotBlank(province), "province", province);
        queryWrapper.eq(StringUtils.isNotBlank(city), "city", city);
        queryWrapper.eq(StringUtils.isNotBlank(street), "street", street);
        queryWrapper.eq(StringUtils.isNotBlank(detail), "detail", detail);
        queryWrapper.eq(StringUtils.isNotBlank(jieyumeasure), "jieyumeasure", jieyumeasure);
        queryWrapper.eq(StringUtils.isNotBlank(nowAddress), "nowAddress", nowAddress);
        queryWrapper.eq(StringUtils.isNotBlank(work), "work", work);
        queryWrapper.eq(StringUtils.isNotBlank(certificateNumber), "certificateNumber", certificateNumber);
        queryWrapper.eq(StringUtils.isNotBlank(accountType), "accountType", accountType);
        queryWrapper.eq(stayTime != null, "stayTime", stayTime);
        queryWrapper.eq(StringUtils.isNotBlank(workplace), "workplace", workplace);
        queryWrapper.eq(StringUtils.isNotBlank(district), "district", district);
        queryWrapper.eq(administerId != null, "administerId", administerId);
        queryWrapper.eq(addUserId != null, "addUserId", addUserId);
        queryWrapper.eq(StringUtils.isNotBlank(ethnicity), "ethnicity", ethnicity);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq(idType != null, "idType", idType);
        queryWrapper.eq(childNum != null, "childNum", childNum);
        queryWrapper.eq(StringUtils.isNotBlank(supportNumber), "supportNumber", supportNumber);
        queryWrapper.eq(StringUtils.isNotBlank(mannageStreet), "mannageStreet", mannageStreet);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_DESC),
                sortField);
        return queryWrapper;
    }

    public Date transfer(Integer targetYear) {
        Date resultDate = null;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());                    //放入Date类型数据
            int year = calendar.get(Calendar.YEAR);//获取年份
            int month = calendar.get(Calendar.MONTH);//获取月份
            int day = calendar.get(Calendar.DATE);//获取日
            String format = (year - targetYear) + "-" + (month + 1) + "-" + day;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            resultDate = simpleDateFormat.parse(format);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return resultDate;
    }

    @Override
    public List<OccupantVO> getOccupantVOList(List<Occupant> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getOccupantVO).collect(Collectors.toList());
    }

    @Override
    public boolean excelUpload(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败,网络问题");
        }
        return false;
    }
}




